//
//  TZNewFeatureController.swift
//  WeiboWithSwift
//
//  Created by 涂真 on 15/12/17.
//  Copyright © 2015年 涂真. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class TZNewFeatureController: UICollectionViewController {
    let itemCount = 4
    //流水布局
    private let layout = UICollectionViewFlowLayout()
    //appDelegate调用
    init(){
        super.init(collectionViewLayout: layout)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        prepareCollectionView()
        
    }
    
    //MARK: - 准备collectionView的相关事项
    func prepareCollectionView(){
        
        self.collectionView!.registerClass(TZNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        //设置分页效果
        collectionView?.pagingEnabled = true
        //取消弹簧效果
        collectionView?.bounces = false
        //隐藏水平滚动条
        collectionView?.showsHorizontalScrollIndicator = false
    }
    
    //MARK: - 显示完毕时调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        //获取当前cell的indexPath
        let visivleIndexPath = collectionView.indexPathsForVisibleItems().last!//正在显示的所有indexPath
        if visivleIndexPath.item == itemCount-1{
            //获取指定IndexPath的cell
        let cell = collectionView.cellForItemAtIndexPath(visivleIndexPath) as! TZNewFeatureCell
            //调用体验按钮的动画方法
            cell.experienceBtnAnimation()
        
        }
    }
    
    // MARK: UICollectionViewDataSource
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return itemCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! TZNewFeatureCell
        
        cell.index = indexPath.item
        return cell
    }
    
    
    
}

//MARK: - 自定义cell
class TZNewFeatureCell: UICollectionViewCell {
    
    var index = 0{
        didSet{
            experienceBtn.hidden = true
            backgroundImageView.image = UIImage(named: "new_feature_\(index+1)")
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        appearanceUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MAKR: - 准备UI
    func appearanceUI(){
        addSubview(backgroundImageView)
        addSubview(experienceBtn)
        
        backgroundImageView.translatesAutoresizingMaskIntoConstraints = false
        experienceBtn.translatesAutoresizingMaskIntoConstraints = false
        
        //约束控件
        //背景图片
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: backgroundImageView, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        //体验按钮
        addConstraint(NSLayoutConstraint(item: experienceBtn, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: experienceBtn, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        
    }
    
    //MARK: - 体验按钮动画
    func experienceBtnAnimation(){
    experienceBtn.hidden = false
        experienceBtn.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.5, delay: 0, usingSpringWithDamping: 0.5, initialSpringVelocity: 3, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.experienceBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    
    }
    
    //MARK: - 体验按钮点击
    func  experienceBtnDidClick(){
    let targetController = (UIApplication.sharedApplication().delegate as! AppDelegate).defultViewController()
        (UIApplication.sharedApplication().delegate as! AppDelegate).switchRootController(targetController is TZMainController)
    }
    
    //MARK: - 懒加载
    //背景图片
    private lazy var backgroundImageView:UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //体验按钮
    private lazy var experienceBtn:UIButton = {
        let button = UIButton()
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitle("立即体验", forState: UIControlState.Normal)
        button.addTarget(self, action: "experienceBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return button
    }()
}
